package com.ecarx.vhaltests.hy11.testcases.notify;

import android.car.VehicleAreaType;
import android.car.hardware.CarPropertyValue;
import android.car.hardware.property.CarPropertyManager;
import android.hardware.automotive.vehicle.V2_0.VehicleArea;
import android.hardware.automotive.vehicle.V2_0.VehiclePropertyGroup;
import android.hardware.automotive.vehicle.V2_0.VehiclePropertyType;
import android.util.Log;

import com.ecarx.vhaltests.VehicleConstants;
import com.ecarx.vhaltests.VhalUnitTestBase2;
import com.ecarx.vhaltests.hy11.testcases.Utils.EcuUtil;
import com.ecarx.xui.adaptapi.car.IWrapper;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import vendor.ecarx.xma.automotive.vehicle.V2_0.ECarXVehicleProperty;

//其他设置-电子踏板E-Pedal-文言提示
public class NotifyInfo27Test extends VhalUnitTestBase2 {
    private static final String TAG = NotifyInfo27Test.class.getSimpleName();
    private  Integer[] mCallbackValue;
    private  int mCallbackStatus;
    private static final int POP_ID = 27;
    private static final int POP_STATUS_SHOW = 1;
    private static final int POP_STATUS_CANCEL = 0;//0 or 2 or 3

    @BeforeClass
    public static void testInit() {
        registerConfig();
    }

    @Override
    public void init() {
        getCarPropertyMgr().registerCallback(carPropertyEventCallback, getPropertyId(),CarPropertyManager.SENSOR_RATE_ONCHANGE);
    }

    @Override
    protected int getPropertyId() {
        //NOTIFY_INFO
//        int mPropertyId =   (
//                0x76ff  | VehiclePropertyGroup.VENDOR
//                | VehiclePropertyType.INT32_VEC
//                | VehicleArea.GLOBAL);
//        return mPropertyId;
        return ECarXVehicleProperty.NOTIFY_INFO;
    }

    @Override
    protected int getArea() {
        return VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL;
    }

    @Override
    protected String getValueType() {
        return TYPE_VALUE_BOOLEAN;
    }

    @Test
    public void testSettingFuncEPedalNotifyShow() {
        setCarConfig(465,2);
        setCEM(VehicleConstants.CAR_MODE_NOMAL, VehicleConstants.USG_MODE_DRIVING ,2 ,1);
        lockWait();
        Assert.assertTrue(mCallbackValue != null && mCallbackValue.length > 1);
        Assert.assertEquals(POP_ID, mCallbackValue[1].intValue());
        Assert.assertEquals(POP_STATUS_SHOW, mCallbackValue[0].intValue());
    }

    @Test
    public void testSettingFuncEPedalNotifyCancel() {
        setCarConfig(465,2);
        setCEM(VehicleConstants.CAR_MODE_NOMAL, VehicleConstants.USG_MODE_DRIVING ,2 ,1);
        lockWait();
        Assert.assertTrue(mCallbackValue != null && mCallbackValue.length > 1);
        Assert.assertEquals(26, mCallbackValue[1].intValue());
        Assert.assertEquals(POP_STATUS_CANCEL, mCallbackValue[0].intValue());
    }


    private  final CarPropertyManager.CarPropertyEventCallback carPropertyEventCallback = new CarPropertyManager.CarPropertyEventCallback() {
        @Override
        public void onChangeEvent(CarPropertyValue carPropertyValue) {
            try {
                mCallbackValue = (Integer[]) carPropertyValue.getValue();
                mCallbackStatus = carPropertyValue.getStatus();
                lockNotify();
            } catch (Exception e) {
                Log.e("【test】", e.getMessage());
            }
        }

        @Override
        public void onErrorEvent(int i, int i1) {
        }
    };


    private static void registerConfig() {
        EcuUtil.registerConfigCarConfig(getIpcpClient());
        EcuUtil.registerConfigVDDM(getIpcpClient());
    }


    public void setCarConfig(int configs,int value) {
        Log.d(TAG,"setCarConfig :"+configs +" value:"+value);
        int[] requests = {configs};
        int[] values = {value};
        EcuUtil.setCarConfig(getIpcpClient(), requests, values);
    }

    public void setCEM(int carMode, int usgMode, int intPm25HiPopUp, int climaOffReq) {
        Log.d(TAG,"setCEM carMode :"+carMode +" usgMode:"+usgMode);
        String[] requests = {"vehModMngtGlbSafe1CarModSts1", "vehModMngtGlbSafe1UsgModSts" ,"intPm25HiPopUp","climaOffReq"};
        int[] values = {carMode, usgMode};
        EcuUtil.setCEM(getIpcpClient(), requests, values);
    }
}
